/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vng.zing.survey.memcached;

import com.vng.zing.stdprofile2.thrift.TValue;
import com.vng.zing.survey.thrift.client.SurveyClient;
import com.vng.zing.survey.thrift.model.IDSurveyResult;
import com.vng.zing.survey.thrift.model.SurveyItem;
import com.vng.zing.survey.thrift.model.Error;
import com.vng.zing.survey.thrift.model.SurveyItemResult;
import com.vng.zing.survey.utility.ZingMeService;
import org.apache.log4j.Logger;

/**
 *
 * @author kienpd
 */
public class SurveyCache {
    private static Logger logger = Logger.getLogger(SurveyCache.class);
    public static IDSurveyResult newSurveyWithCache(int idUser, SurveyItem surveyItem) {
        try {
            SurveyClient survey = SurveyClient.getInstance();
            IDSurveyResult id = survey.newSurvey(idUser, surveyItem);
            if (id.getError() == Error.SUCCESS) {
//                CacheFactory.setSurveyMemCache(id.getIdSurvey(), surveyItem);
            }
            return id;
        } catch (Exception ex) {
           logger.error(ex.toString());
        }
        return new IDSurveyResult(0, Error.ERR_SYSTEM);
    }

    public static Error updateSurveyWithCache(int idSurvey, SurveyItem surveyItem) {
        try {
           
            SurveyClient survey = SurveyClient.getInstance();

            Error error = survey.updateSurvey(idSurvey, surveyItem);
           
            if (error == Error.SUCCESS) {
                CacheFactory.setSurveyMemCache(idSurvey, surveyItem);
            }
            return error;

        } catch (Exception ex) {
           logger.error(ex.toString());
        }
        return Error.ERR_SYSTEM;

    }

    public static SurveyItemResult getSurveyWithCache(int idSurvey) {
        SurveyItemResult itemResult = null;
        try {

            SurveyItem surveyItem = (SurveyItem) CacheFactory.getSurveyMemCache(idSurvey);
            if (surveyItem == null) {
              
                SurveyClient survey = SurveyClient.getInstance();
                itemResult = survey.getSurvey(idSurvey);
                if (itemResult.getErro() == Error.SUCCESS) {
                    CacheFactory.setSurveyMemCache(idSurvey, itemResult.getSurveyItem());
                    
                }
            } else {
                itemResult = new SurveyItemResult(Error.SUCCESS, surveyItem);

            }
            return itemResult;
        } catch (Exception ex) {
           logger.error(ex.toString());
        }
        return new SurveyItemResult(Error.ERR_SYSTEM, null);

    }

    public static TValue getproflieUserWithCache(String nameUser) {
        TValue user = (TValue) CacheFactory.getUserByNameMemCache(nameUser);
        if (user == null) {
            user = ZingMeService.getproflieUser(nameUser);
            if (user != null) {
                CacheFactory.setUserByNameMemCache(nameUser, user);
            }
        }
        return user;

    }

    public static Error removeSurveyWithCache(int idSurvey, int idUser) {
        Error error;
        try {
            SurveyClient survey = SurveyClient.getInstance();
            error = survey.removeSurveyItem(idSurvey, idUser);
            CacheFactory.deleteSurveyMemCache(idSurvey);
            return error;
        } catch (Exception ex) {
            logger.error(ex.toString());
        }
        return Error.ERR_SYSTEM;
    }
}
